/**
 * @author Guy Sagy
 */

package model;

import java.io.Serializable;
import java.util.TreeMap;

// Class invariant : all profile properties are non-null
public class Profile implements Serializable
{
    private TreeMap<String, String>  data = new TreeMap<String, String>();

    public Profile(int userId, String firstName, String lastName, String emailAddress)
    {
        setFirstName(firstName);
        setLastName(lastName);
        setEmailAddress(emailAddress);
    }

    public final void setUserId(int userId)
    {
        data.put("userId", Integer.toString(userId));
    }

    public final int getUserId()
    {
        return Integer.parseInt(data.get("userId"));
    }

    public final void setFirstName(String firstName)
    {
        firstName = firstName != null ? firstName : "";
        data.put("firstName", firstName);
    }

    public String getFirstName()
    {
        return data.get("firstName");
    }

    public final void setLastName(String lastName)
    {
        lastName = lastName != null ? lastName : "";
        data.put("lastName", lastName);
    }

    public String getLastName()
    {
        return data.get("lastName");
    }

    public final void setEmailAddress(String emailAddress)
    {
        emailAddress = emailAddress != null ? emailAddress : "";
        data.put("emailAddress", emailAddress);
    }

    public String getEmailAddress()
    {
        return data.get("emailAddress");
    }

    public static boolean equals(Profile lProfile, Profile rProfile)
    {
        return lProfile.equals(rProfile);
    }

    @Override
    public int hashCode()
    {
        return (this.getFirstName().hashCode()
                + this.getLastName().hashCode()
                + this.getEmailAddress().hashCode())/4;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }

        if (getClass() != obj.getClass())
        {
            return false;
        }

        final Profile other = (Profile) obj;

        // TODO: revisit this implementation
        if (this.data != other.data && (this.data == null || !this.data.equals(other.data)))
        {
            return false;
        }

        return true;
    }
}